﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rem.Objects;

namespace FileDB
{
    public partial class Contract
    {
        public Contract()
        {
            Id = QueryConstants.UnsavedObjectId;
            Status = ReminderState.Actived;
            StartDate = DateTime.Now.Date;
            DueDate = DateTime.Now.AddYears(1).Date;
            SignTime = DateTime.Now.Date;
            AdvancedDays = 7;
            AdvancedRenew = 20;
            RentInterval = 3;
            RentPerInterval = 10000;
            Deposit = 10000 * 3;
            Address = "上海市";
            ContractNo = DateTime.Now.ToString("yyyy-MM-dd");
            PartAId = Customer.GetSelfCompany().Id;
            PartBId = Customer.GetSelfCompany().Id;
            RentUnit = Contract.RentUnit_Month;
        }

        public int Id { get; set; }
        public string Address { get; set; }
        public decimal AreaSize { get; set; }
        public int PartAId { get; set; }
        public int EmployeeId { get; set; }
        public DateTime SignTime { get; set; }
        /// <summary>
        /// total rent
        /// </summary>
        public double TotalRent
        {
            get
            {
                if (this.RentUnit == Contract.RentUnit_Month)
                {

                    return (long)(
                        this.RentPerInterval * 12 * (int)((this.DueDate.Date - this.StartDate.Date).TotalDays / 365) +
                        this.RentPerInterval * (int)((this.DueDate.Date - this.StartDate.Date).TotalDays % 365) / 30.0);
                }
                else if (this.RentUnit == Contract.RentUnit_Year)
                {
                    return (long)(
                        this.RentPerInterval * (int)((this.DueDate.Date - this.StartDate.Date).TotalDays / 365) +
                        this.RentPerInterval * ((this.DueDate.Date - this.StartDate.Date).TotalDays % 365)/365);
                }
                else
                {
                    return 0.0;
                }

            }
        }

        public string RentInfo
        {
            get
            {
                return string.Format("{0} {1}", this.RentPerInterval.ToString("###,###,##0"), this.RentUnit);
            }
        }
        // 缴租，收租周期，* 月
        public int RentInterval { get; set; }
        // 元/月，元/年
        private string _rentUnit;

        public string RentUnit
        {
            get
            {
                if (string.IsNullOrEmpty(_rentUnit))
                    _rentUnit = RentUnit_Month;
                return _rentUnit;
            }
            set { _rentUnit = value; }
        }

        public double RentInYear
        {
            get
            {
                if (this.RentUnit == Contract.RentUnit_Month)
                {
                    return this.RentPerInterval * 12;
                }
                else if (this.RentUnit == Contract.RentUnit_Year)
                {
                    return this.RentPerInterval;
                }
                else
                {
                    return 0.0;
                }
            }
        }
        //一个周期内的还款
        public double RentInInterval
        {
            get
            {
                if (this.RentUnit == Contract.RentUnit_Month)
                {
                    return this.RentPerInterval * this.RentInterval;
                }
                else if (this.RentUnit == Contract.RentUnit_Year)
                {
                    return this.RentPerInterval * this.RentInterval / 12;
                }
                else
                {
                    return 0.0;
                }
            }
        }

        public const string RentUnit_Month = "元/月";
        public const string RentUnit_Year = "元/年";

        //租金，年或则月
        public double RentPerInterval { get; set; }
        public double Deposit { get; set; }
        public int AdvancedDays { get; set; }
        public int FreeDaysIn { get; set; }
        public int FreeDaysOut { get; set; }
        public DateTime StartDate { get; set; }
        public string StartDateString { get { return this.StartDate.ToString("yyyy-MM-dd"); } }
        public DateTime DueDate { get; set; }
        public string DueDateString { get { return this.DueDate.ToString("yyyy-MM-dd"); } }
        public string Comment { get; set; }
        public string ContractNo { get; set; }


        public int PartBId { get; set; }
        public decimal Width { get; set; }
        //public string SubmitRentDate { get; set; }
        public bool CanTicket { get; set; }
        public int AdvancedRenew { get; set; }
        public string Status { get; set; }


        public Customer CustomerPartA
        {
            get
            {
                if (PartAId > 0)
                {
                    return Customer.GetCustomer(PartAId);
                }
                return null;
            }
        }

        public Customer CustomerPartB
        {
            get
            {
                if (PartBId > 0)
                {
                    return Customer.GetCustomer(PartBId);
                }
                return null;
            }
        }
        public List<Reminder> Reminders
        {
            get
            {
                return Reminder.GetReminder(this.Id);
            }
        }

        public static void ContractEnable(int p, bool p_2)
        {
            throw new NotImplementedException();
        }
        public static object ThisYearContracts()
        {
            throw new NotImplementedException();
        }

        public static object LastMonthContracts()
        {
            throw new NotImplementedException();
        }

        public static object ThisMonthContracts()
        {
            throw new NotImplementedException();
        }

        public void Delete()
        {
            Contract.Delete(this);
        }

        public static Contract GetContract(int p)
        {
            if (p < 1)
            {
                return null;
            }
            return Contract.GetData(GetFileName(p));
        }

    }
}
